************************************************************
****************** REPLICATION CODE ************************
************************************************************
* For: Rigterink, The Wane of Command, APSR				   *
* Randomization inference								   *
************************************************************
* Content of file:										   *
/*														   *
1. Preliminaries and setting directory					   *
2. Create dataset with dependent variable only			   *
3. Main results											   *
4. Randomization inference								   *
5. Graph of results										  */
************************************************************

/*----------------------------------------------------------
---------- Preliminaries and setting directory -------------
----------------------------------------------------------*/

clear all
version 14
set more off

*******************************
*** Set your directory here ***
*******************************
cd "[directory]"

cap log close
log using log\Rigterink_drones_replication_appendix_randinf.log, replace


/*----------------------------------------------------------
------ Create dataset with dependent variable only ---------
----------------------------------------------------------*/

use "dta/Rigterink_drones_replication_maindataset.dta", clear

* Create dataset with dependent variable only
keep terratt groupid
save "dta/Rigterink_drones_replication_depvaronly.dta", replace

use "dta/Rigterink_drones_replication_maindataset.dta", clear


/*----------------------------------------------------------
--------------------- Main results -------------------------
----------------------------------------------------------*/

reghdfe terratt L(0/6).bigfishdied L(0/6).bigfishtarget L(0/6).dronestrike F(1/6).bigfishtarget F(1/6).bigfishdied F(1/6).dronestrike, absorb(i.groupid i.periodid) vce(, bw(12))

matrix b = e(b)
matrix coefs = J(1, 6, .)
	
matrix coefs[1, 1]=b[1,2]
matrix coefs[1, 2]=b[1,3]
matrix coefs[1, 3]=b[1,4]
matrix coefs[1, 4]=b[1,5]
matrix coefs[1, 5]=b[1,6]
matrix coefs[1, 6]=b[1,7]
	

/*----------------------------------------------------------
---------------- Randomization inference -------------------
----------------------------------------------------------*/

local N = 10000

* Create matrix
set matsize 10000
matrix Fstats = J(`N', 6, .)

forvalues R = 1(1)`N' {
	
	di "Round `R'"
	
	local seed = 19860223 + `R'
	
	set seed `seed'
	
	use "dta/Rigterink_drones_replication_depvaronly.dta", clear
	
	* Allocation dependent variable to random period
	gen rand = runiform()
	sort rand
	bysort groupid: gen periodid = _n
	qui replace periodid = periodid+47 if groupid==9
	qui replace periodid = periodid+92 if groupid==7
	
	* Merging other data back in 
	qui merge 1:1 groupid periodid using "dta/Rigterink_drones_replication_maindataset.dta", keepusing(bigfishtarget bigfishdied dronestrike) nogen
	
	qui xtset groupid periodid
	
	* Producing results
	qui reghdfe terratt L(0/6).bigfishdied L(0/6).bigfishtarget L(0/6).dronestrike F(1/6).bigfishdied F(1/6).bigfishtarget F(1/6).dronestrike, absorb(i.groupid i.periodid) vce(, bw(12))
	
	matrix b = e(b)
	
	* Storing coefficients in matrix
	matrix Fstats[`R', 1]=b[1,2]
	matrix Fstats[`R', 2]=b[1,3]
	matrix Fstats[`R', 3]=b[1,4]
	matrix Fstats[`R', 4]=b[1,5]
	matrix Fstats[`R', 5]=b[1,6]
	matrix Fstats[`R', 6]=b[1,7]
	
}
	
* Converting matrix to dataset
clear
svmat Fstats
rename (Fstats1 Fstats2 Fstats3 Fstats4 Fstats5 Fstats6) (L1 L2 L3 L4 L5 L6)

save dta/Rigterink_drones_replication_randinf.dta, replace


/*----------------------------------------------------------
-------------------- Graph of results ----------------------
----------------------------------------------------------*/

use dta/Rigterink_drones_replication_randinf.dta, clear

local N = 10000

forvalues i = 1(1)6 {
	local coef = coefs[1, `i']
	di `coef'
	
	sum L`i' if L`i'>`coef'
	local larger = r(N)/`N'*100
	local max = r(max)
	
	graph twoway (hist L`i', color(white) lcolor(gray)) ///
	(kdensity L`i', lcolor(black)), ///
	xline(`coef', lpattern(dash) lcolor(black)) ///
	plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) ///
	title("hit{subscript:t+`i'}", color(black)) ///
	ytitle("Density") xtitle("Simulated coefficients") ///
	legend(label(1 "Observed") label(2 "Kernel Density")) ///
	text(1.5 `coef'  "`: di %9.4f `coef''", place(e) margin(small)) ///
	text(0.2 `max' "`larger'%", place(w) box margin(small) fcolor(white))

	graph save graphs/combine_randinf`i', replace
}

* Combining graphs

grc1leg graphs/combine_randinf1.gph graphs/combine_randinf2.gph graphs/combine_randinf3.gph graphs/combine_randinf4.gph graphs/combine_randinf5.gph graphs/combine_randinf6.gph, rows(3) ///
graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) ///
title("Randomization inference", color(black)) ///
subtitle("Dependent variable allocated to random period within group", size(small)) 

graph display, ///
xsize(8.3) ysize(11.7)

graph export graphs/Rigterink_AFigB10.pdf, replace


*******************
*** END OF FILE ***
*******************
